package com.tg.app.activity.device.settings;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SettingAdapter;
import com.tg.data.bean.DeviceSettingsInfo;
import com.appbase.custom.base.SettingData;
import com.tg.app.camera.Camera;
import com.tange.module.camera.hub.CameraHub;
import com.tg.data.media.OnICameraListener;
import com.appbase.custom.constant.RecordModeConstants;
import com.tg.app.R;

import com.tg.app.camera.AVIOCTRLDEFs;

import java.util.ArrayList;

import static com.tg.app.activity.device.DeviceSettingsActivity.EXT_DEVICE_INFO;


public class RecordModeActivity extends DeviceSettingsBaseActivity implements OnICameraListener {


    private static final String TAG = "CloudResolutionActivity";

    private Camera  camera ;

    private DeviceSettingsInfo info;
    private ArrayList<SettingData> datas = new ArrayList<>();
    private SettingAdapter adapter ;
    private int clickposition;
    private  int selectRecordType  =0;

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_record_mode);
        hideActionBar();
        info = getIntent().getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        camera = info == null? null : CameraHub.getInstance().getCamera(info.uuid);
        if (camera != null){
            camera.registerICameraListener(this);
        }

        initView();
        modifyToolBar();
        getStream();
    }

    public void initView(){

        RelativeLayout toolbar = findViewById(R.id.toolbar);
        TextView title = toolbar.findViewById(R.id.device_name);
        title.setText(getString(R.string.record_mode));
        ImageButton back = toolbar.findViewById(R.id.back_toolbar);
        ListView listView = findViewById(R.id.record_mode);
        adapter = new SettingAdapter(datas,this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //setFlag(position);
                clickposition=position;
                startSendui();
                switch (position) {
                    case RecordModeConstants.RECORD_MODE_OFF:
                        setVideoMode(AVIOCTRLDEFs.AVIOTC_RECORDTYPE_OFF);
                        break;
                    case RecordModeConstants.RECORD_MODE_ALARM:
                        setVideoMode(AVIOCTRLDEFs.AVIOTC_RECORDTYPE_ALARM);
                        break;
                    case RecordModeConstants.RECORD_MODE_FULLTIME:
                         setVideoMode(AVIOCTRLDEFs.AVIOTC_RECORDTYPE_FULLTIME);
                        break;
                }
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                onBackBtn();
            }
        });
    }


    @Override
    protected void onSetupFailed() {

    }
    private void onBackBtn(){
        Intent intent = new Intent();
        intent.putExtra(EXT_DEVICE_INFO, info);
        setResult(0,intent);
        finish();
    }


    public void setVideoMode(int type){
        selectRecordType = type;
        camera.sendIOCtrl(AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SETRECORD_REQ,AVIOCTRLDEFs.SMsgAVIoctrlSetVideoModeReq.parseContent(0, selectRecordType, (byte)0));
    }

    private void getStream() {
        int recordType = info.recordType;
        SettingData closeStr = new SettingData(getString(R.string.close), 0);
        SettingData eventStr = new SettingData(getString(R.string.event_record), 0);
        SettingData fulltimeStr = new SettingData(getString(R.string.continuous_record), 0);
        datas.add(closeStr);
        datas.add(eventStr);
        datas.add(fulltimeStr);
        if (recordType >= 0 && recordType <= datas.size()) {
            datas.get(recordType).setFlag(1);
        }
        adapter.notifyDataSetChanged();
    }
    private void setFlag(int position){
        for(int i = 0;i<datas.size();i++){
            if(i == position){
                datas.get(i).setFlag(1);
            }else{
                datas.get(i).setFlag(0);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        onBackBtn();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (camera!=null){
            camera.registerICameraListener(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (camera!=null){
            camera.unregisterICameraListener(this);
        }
    }

    @Override
    public void receiveUpdateConnectStates(int state) {

    }

    @Override
    public void receiveIOCtrlData(int type, byte[] data) {
        if (type==AVIOCTRLDEFs.IOTYPE_USER_IPCAM_SETRECORD_RESP){
            byte result=data[3];
            if (result==0){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        completeSend();
                        setFlag(clickposition);
                        info.recordType = selectRecordType;
                        sendUpdateSettingBroadcast(info);
                    }
                });
            }
        }
    }


}
